﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using kxcxw.Data.Entities;
using kxcxw.Data;
using Webdiyer.WebControls.Mvc;
using kxcxw.Data.Common;

namespace KXCXW.Web.Areas.Admin.Controllers
{
    public class UserInfoController : Controller
    {
        private KXCXWDBContext db = new KXCXWDBContext();

        //
        // GET: /UserInfo/

        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult Index(string sortOrder, int? page = 1)
        {
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
            var userinfo = from s in db.SysUserInfoes select s;
            switch (sortOrder)
            {
                case "Name desc":
                    userinfo = userinfo.OrderByDescending(s => s.UserName);
                    break;
                case "Date":
                    userinfo = userinfo.OrderBy(s => s.RegistrDate);
                    break;
                case "Date desc":
                    userinfo = userinfo.OrderByDescending(s => s.RegistrDate); break;
                default:
                    userinfo = userinfo.OrderBy(s => s.RegistrDate);
                    break;
            }
            int pageSize = 10;
            int pageIndex = page ?? 1;
            return View(userinfo.ToPagedList(pageIndex, pageSize));
        }

        //
        // GET: /UserInfo/Details/5
        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult Details(string id)
        {
            SysUserInfo sysuserinfo = db.SysUserInfoes.Find(id);
            SysItemDetial sid = (from q in db.SysItemDetials
                                 where q.ItemDeitalID == sysuserinfo.UserType
                                 select q).FirstOrDefault();
            sysuserinfo.UserType = sid.itemDetialName;
            return View(sysuserinfo);
        }

        //
        // GET: /UserInfo/Create
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Create()
        {
            ViewBag.UserID = new SelectList(db.SysUserProfiles, "UserID", "UserID");
            List<SysItemDetial> userTypelist = db.SysItemDetials.Where(q => q.ItemID == DataConstants.UserTypeItemID).ToList<SysItemDetial>();
            var sl = from c in userTypelist
                     select new
                     {
                         Name = c.itemDetialName,//值
                         Id = c.ItemDeitalID,//健
                     };
            ViewBag.userTypeDropdownlist = new SelectList(sl, "Id", "Name", "C862523F-1034-4473-B306-3B3EDB876F77");
            return View();
        }

        //
        // POST: /UserInfo/Create
        [Authorize(Roles = DataConstants.Admin)]
        [HttpPost]
        public ActionResult Create(SysUserInfo sysuserinfo)
        {
            if (ModelState.IsValid)
            {
                sysuserinfo.UserID = Guid.NewGuid().ToString();
                sysuserinfo.RegistrDate = System.DateTime.Now;
                db.SysUserInfoes.Add(sysuserinfo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.UserID = new SelectList(db.SysUserProfiles, "UserID", "UserID", sysuserinfo.UserID);
            return View(sysuserinfo);
        }

        //
        // GET: /UserInfo/Edit/5
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Edit(string id)
        {
            SysUserInfo sysuserinfo = db.SysUserInfoes.Find(id);
            sysuserinfo.ConfirmPassword = sysuserinfo.UserPwd;
            ViewBag.UserID = new SelectList(db.SysUserProfiles, "UserID", "UserID", sysuserinfo.UserID);
            List<SysItemDetial> userTypelist = db.SysItemDetials.Where(q => q.ItemID == "52FF4ABB-CE8E-44C8-AF97-87D811A99866").ToList<SysItemDetial>();
            var sl = from c in userTypelist
                     select new
                     {
                         Name = c.itemDetialName,//值
                         Id = c.ItemDeitalID,//健
                     };
            ViewBag.userTypeDropdownlist = new SelectList(sl, "Id", "Name", sysuserinfo.UserType);
            return View(sysuserinfo);
        }

        //
        // POST: /UserInfo/Edit/5
        [Authorize(Roles = DataConstants.Admin)]
        [HttpPost]
        public ActionResult Edit(SysUserInfo sysuserinfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sysuserinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserID = new SelectList(db.SysUserProfiles, "UserID", "UserID", sysuserinfo.UserID);
            List<SysItemDetial> userTypelist = db.SysItemDetials.Where(q => q.ItemID == "52FF4ABB-CE8E-44C8-AF97-87D811A99866").ToList<SysItemDetial>();
            var sl = from c in userTypelist
                     select new
                     {
                         Name = c.itemDetialName,//值
                         Id = c.ItemDeitalID,//健
                     };
            ViewBag.userTypeDropdownlist = new SelectList(sl, "Id", "Name", sysuserinfo.UserType);
            return View(sysuserinfo);
        }

        //
        // GET: /UserInfo/Delete/5
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Delete(string id)
        {
            SysUserInfo sysuserinfo = db.SysUserInfoes.Find(id);
            db.SysUserInfoes.Remove(sysuserinfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [Authorize(Roles = DataConstants.Admin)]
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}